package tk.mybatis.springboot.common;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.atomic.AtomicLong;

public class CustomIdGenerator {
   private static final AtomicLong counter = new AtomicLong(0);

   private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");

   public static String generateId(String businessCode){
       String timestamp = LocalDateTime.now().format(formatter);
       long count = counter.incrementAndGet();
       //id = 时间戳+业务代码+计数器值
       String result = timestamp+businessCode+String.format("%03d",count%1000);
       return result;
   }

    public static void main(String[] args) {
        String taskId = generateId("task");
        System.out.println(taskId);
    }
}
